package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.pojo.vo.CodeRunResultVO;
import com.zenithmind.coding.service.CodeAnalysisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 代码分析控制器 - 遵循单一职责原则，专门处理代码分析相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "代码分析", description = "代码质量分析和相似度检测相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/code/analysis")
@RequiredArgsConstructor
public class CodeAnalysisController {

    private final CodeAnalysisService codeAnalysisService;

    @Operation(summary = "分析代码质量", description = "分析代码质量并给出建议")
    @PostMapping("/quality")
    public Result<CodeRunResultVO.CodeQualityAnalysis> analyzeCodeQuality(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        CodeRunResultVO.CodeQualityAnalysis analysis = codeAnalysisService.analyzeCodeQuality(language, sourceCode);
        return Result.success(analysis);
    }

    @Operation(summary = "检测代码相似度", description = "检测两段代码的相似度")
    @PostMapping("/similarity")
    public Result<Double> detectCodeSimilarity(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码1") @RequestParam String sourceCode1,
            @Parameter(description = "源代码2") @RequestParam String sourceCode2) {
        Double similarity = codeAnalysisService.detectCodeSimilarity(language, sourceCode1, sourceCode2);
        return Result.success(similarity);
    }

    @Operation(summary = "检测代码抄袭", description = "检测代码是否存在抄袭")
    @PostMapping("/plagiarism")
    public Result<Boolean> detectPlagiarism(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode,
            @Parameter(description = "相似度阈值") @RequestParam(defaultValue = "0.8") Double threshold) {
        Boolean isPlagiarism = codeAnalysisService.detectPlagiarism(language, sourceCode, threshold);
        return Result.success(isPlagiarism);
    }

    @Operation(summary = "分析代码复杂度", description = "分析代码的圈复杂度")
    @PostMapping("/complexity")
    public Result<Integer> analyzeComplexity(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Integer complexity = codeAnalysisService.analyzeComplexity(language, sourceCode);
        return Result.success(complexity);
    }

    @Operation(summary = "代码风格检查", description = "检查代码风格是否符合规范")
    @PostMapping("/style")
    public Result<Object> checkCodeStyle(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Object styleReport = codeAnalysisService.checkCodeStyle(language, sourceCode);
        return Result.success(styleReport);
    }

    @Operation(summary = "代码安全检查", description = "检查代码中的安全漏洞")
    @PostMapping("/security")
    public Result<Object> checkSecurity(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Object securityReport = codeAnalysisService.checkSecurity(language, sourceCode);
        return Result.success(securityReport);
    }

    @Operation(summary = "代码性能分析", description = "分析代码的性能特征")
    @PostMapping("/performance")
    public Result<Object> analyzePerformance(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Object performanceReport = codeAnalysisService.analyzePerformance(language, sourceCode);
        return Result.success(performanceReport);
    }

    @Operation(summary = "获取代码建议", description = "获取代码改进建议")
    @PostMapping("/suggestions")
    public Result<Object> getCodeSuggestions(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Object suggestions = codeAnalysisService.getCodeSuggestions(language, sourceCode);
        return Result.success(suggestions);
    }
}
